﻿using System;
using System.Collections.Generic;
using N2.Engine.Globalization;
using FlexiCommerce.Web.Templates.Layouts;
using N2.Collections;
using N2;
using FlexiCommerce.Web.Templates.Web;

namespace FlexiCommerce.Web.Templates.UI.Layouts
{
    public partial class TopMenu : Web.UI.TemplateUserControl<ContentItem>
    {
        public int MaxLevels
        {
            get { return tm.MaxLevels; }
            set { tm.MaxLevels = value; }
        }

        protected ILanguageGateway languages;
        protected override void OnInit(EventArgs e)
        {
            languages = Engine.Resolve<ILanguageGateway>();

            if (languages.Enabled)
            {
                ITranslationProvider translationProvider = CurrentPage as ITranslationProvider;
                if (translationProvider == null)
                {
                    rptLanguages.DataSource = GetTranslations();
                }
                else
                {
                    rptLanguages.DataSource = translationProvider.GetTranslations();
                }

                DataBind();

                rptLanguages.Visible = rptLanguages.Items.Count > 0;
            }
            else
            {
                rptLanguages.Visible = false;
            }
            base.OnInit(e);
        }

        private IEnumerable<Translation> GetTranslations()
        {
        	ItemFilter languageFilter = new CompositeFilter(new AccessFilter(), new PublishedFilter());
        	IEnumerable<ContentItem> translations = languages.FindTranslations(CurrentPage);
			foreach (ContentItem translation in languageFilter.Pipe(translations))
			{
				ILanguage language = languages.GetLanguage(translation);
				
				// Hide translations when filtered access to their language
				ContentItem languageItem = language as ContentItem;
				if(languageItem == null || languageFilter.Match(languageItem))
					yield return new Translation(translation, language);
			}
        }
    }
}